import QtQuick 1.1
import com.nokia.meego 1.0

import "DucankoStyle.js" as DucankoStyle

// ViewListPage
Page {

    id: viewListPage

    property string currentListName: "current list name"
    property int currentListIndex
    property int listsCount

    property int groupIndex : 0

    property variant itemDetailsDialog

    property variant messageBox

    Image {
        id: background
        anchors.fill: parent
        source: "qrc:/images/pozadina1.png"
        fillMode: "Stretch"
    }


    //sectionCenter
    Item {
        id: sectionCenter

        anchors {
            horizontalCenter: parent.horizontalCenter
            top: sectionTop.bottom
        }

        height: parent.height-sectionBottom.height-sectionTop.height
        width: parent.width

        ListView {

            id: list
            anchors.fill: parent
            highlight: Rectangle {
                color: "lightsteelblue"
                radius: 5
                width: list.width
            }
            highlightMoveSpeed: -1
            model: modelList


            currentIndex: -1
            delegate: Rectangle {
                height: isAGroup?50:70
                width: parent.width

                property int groupnum: groupNumber

                color: isAGroup?(groupnum%5===1?"#FF931E":groupnum%5===2?"#3EA8F4":groupnum%5===3?"#79C842":
                                 groupnum%5===4?"#FE1C24":"#FE7AAB"):"transparent"


                //color: isAGroup?(groupnum%5===1?"#4278c6":groupnum%5===2?"#79C842":groupnum%5===3?"#c64277":
                //                 groupnum%5===4?"#A1E371":"#6A964B"):"transparent"


                //color: isAGroup?((groupnum%3===1)?"#79C842":groupnum%3===2?"#E3B34B":"#3EA8F4"):"transparent"

                Image {
                    id: lvImage
                    height: parent.height - 20
                    width: isAGroup?0:height
                    source: isAGroup?"":bought?"qrc:/images/kupljeno.png":"qrc:/images/prazno3.png" //TU

                    fillMode: Image.PreserveAspectFit
                    anchors {
                        left: parent.left
                        verticalCenter: parent.verticalCenter
                        leftMargin: 10
                        rightMargin: 10
                    }
                }
                Text {
                    id: txtLeft
                    anchors {
                        left: lvImage.right
                        verticalCenter: parent.verticalCenter
                        leftMargin: 10
                    }
                    text: isAGroup?groupName:name

                    font.family: DucankoStyle.FONT_FAMILY
                    font.pixelSize: isAGroup?DucankoStyle.FONT_LIST_GROUP_SIZE:DucankoStyle.FONT_LIST_ITEM_SIZE
                    font.bold: isAGroup

                }
                Text{
                    property int qnty
                    property string unitstr
                    id: txtRight
                    anchors {
                        left: txtLeft.right
                        right: parent.right
                        verticalCenter: parent.verticalCenter
                        rightMargin: 10
                    }
                    horizontalAlignment: Text.AlignRight

                    qnty: Math.round(quantity*10)

                    unitstr: (unit==='kom')?qsTr("kom"):unit

                    text: isAGroup ? "" : qnty/10+" " + unitstr //inace su nam se decimalni prikazivali na milijun decimala

                    font.family: DucankoStyle.FONT_FAMILY
                    font.pixelSize: DucankoStyle.FONT_LIST_QUANTITY_SIZE
                }

                MouseArea {
                    id: mArea
                    anchors.fill: parent
                    onClicked: {
                        list.currentIndex = isAGroup?-1:index
                    }
                    onDoubleClicked: {
                        list.currentIndex = isAGroup?-1:index

                        if (list.currentIndex > 0)
                            buttonBought.buttonClicked()
                    }

                    onPressed: {
                        list.currentIndex = isAGroup?-1:index
                    }

                    onPressAndHold: {
                        list.currentIndex = isAGroup?-1:index

                        if (list.currentIndex > 0)
                            buttonEdit.buttonClicked()
                    }
                }
            }
        }

        Text{
            id: textNoItems
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter: parent.verticalCenter

            width: parent.width

            font.family: DucankoStyle.FONT_FAMILY
            font.pixelSize: DucankoStyle.FONT_BIG_TITLE_SIZE

            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter

            wrapMode: Text.Wrap

            text: qsTr("This list is still empty.")
            visible: modelList.count<1&&groupIndex==0
        }
        Text{
            id: textNoItems2
            anchors.top: textNoItems.bottom
            anchors.left: parent.left

            width: parent.width

            font.family: DucankoStyle.FONT_FAMILY
            font.pixelSize: DucankoStyle.FONT_MESSAGE_SIZE

            anchors.horizontalCenter: parent.horizontalCenter

            text: qsTr("Use the + button below to add items!")
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter

            wrapMode: Text.Wrap

            visible: modelList.count<1&&groupIndex==0
        }

    }

    //sectionTop
    Item {
        id: sectionTop

        anchors {
            horizontalCenter: parent.horizontalCenter

            top: parent.top
            left: parent.left

            //margins: 10
        }

        height: sectionLabel.height+buttonGrupa.height
        width: parent.width

        Image{
            id: bgTop
            anchors.fill: parent
            source: "qrc:/images/diopozadine-01gore.png"
            fillMode: "Stretch"
        }

        Item{
            id: sectionLabel
            anchors{
                top:parent.top
                left:parent.left
                horizontalCenter: parent.horizontalCenter
                leftMargin: 10
                rightMargin: 10
            }
            height: label.height*3

            RoundButton {
                id: roundButtonLeft

                anchors.left: parent.left
                anchors.verticalCenter: parent.verticalCenter

                width: row.itemWidth; height: row.itemHeight;
                imgSource: "qrc:/images/lijevo.png";
                imgPressedSource: "qrc:/images/lijevo2.png";
                imgDisabledSource: "qrc:/images/lijevo3.png";

                isEnabled: listsCount>1

                function buttonClicked(){
                    currentListIndex--
                    if (currentListIndex<0)
                        currentListIndex+=listsCount
                    currentListIndex%=listsCount
                    dialogGrupa.selectedIndex=0
                    buttonGrupa.text=qsTr("All groups")
                    qmlHandler.refreshCurrentList(currentListIndex,-1)
                    currentListName=modelAllLists[currentListIndex].name
                }
            }

            RoundButton {
                id: roundButtonRight

                anchors.right: parent.right
                anchors.verticalCenter: parent.verticalCenter

                width: row.itemWidth; height: row.itemHeight;
                imgSource: "qrc:/images/desno.png";
                imgPressedSource: "qrc:/images/desno2.png";
                imgDisabledSource: "qrc:/images/desno3.png";

                isEnabled: listsCount>1

                function buttonClicked(){
                    currentListIndex++
                    currentListIndex%=listsCount
                    dialogGrupa.selectedIndex=0
                    buttonGrupa.text=qsTr("All groups")
                    qmlHandler.refreshCurrentList(currentListIndex,-1)
                    currentListName=modelAllLists[currentListIndex].name
                }
            }

            Label {
                id: label
                anchors.centerIn: parent
                //anchors.left: parent.right
                //lineHeight: 1
                //width: parent.width - roundButtonLeft.width - roundButtonRight.width
                //horizontalAlignment: Text.AlignHCenter
                text: currentListName

                font.family: DucankoStyle.FONT_FAMILY
                font.pixelSize: DucankoStyle.FONT_PAGE_TITLE_SIZE
            }
        }

        Button {
            id: buttonGrupa
            text: qsTr("All groups");
            anchors{
                top: sectionLabel.bottom
                left: parent.left
                horizontalCenter: parent.horizontalCenter
            }

            width: parent.width;

            onClicked: {
                dialogGrupa.open();
            }

            ToolIcon {
                id: icon
                anchors {
                    right: parent.right
                    verticalCenter: parent.verticalCenter
                }
                iconId: "icon-m-toolbar-down"
                onClicked: dialogGrupa.open();

            }

            style: ButtonStyle{ horizontalAlignment: Text.AlignLeft }
        }

    }




    //sectionBottom
    Item {
        id: sectionBottom

        anchors {
            horizontalCenter: parent.horizontalCenter
            left: parent.left
            //top: buttonGrupa.bottom
            bottom: parent.bottom
        }

        width: parent.width
        height: sectionButtonsRound.height+sectionButtonsNormal.height+
                sectionButtonsRound.anchors.margins*3//+((sectionItemDetails.visible)?sectionItemDetails.height:0)


        //bgBottom
        Image{
            id: bgBottom
            anchors.fill: parent
            source: "qrc:/images/diopozadine-02dolje.png"
            fillMode: "Stretch"
        }

        //sectionButtonsRound
        Item{
            id: sectionButtonsRound

            height: row.itemHeight
            width: parent.width-row.columns*row.itemWidth //ovo ovisi o sirini gumbi

            //color: "blue"

            anchors {
                horizontalCenter: parent.horizontalCenter

                //top: parent.top
                bottom: sectionButtonsNormal.top
                left: parent.left

                margins: 10
            }

            Grid {
                id: row

                /*anchors.horizontalCenter: parent.horizontalCenter
                anchors.top: parent.top
                anchors.left: parent.left*/
                anchors.fill: parent
                anchors.horizontalCenter: parent.horizontalCenter

                //anchors.margins: 10

                width: parent.width
                height: parent.height

                rows: 1
                columns: 5

                property int itemWidth: 80//Math.round((width-columns*spacing)/columns)//parent.height-2*spacing; //Math.round((width - spacing * (columns - 1)) / columns)
                property int itemHeight: 80 //itemWidth //Math.round((height - spacing * (rows - 1)) / rows)

                spacing: (width-columns*itemWidth)/(columns-1)

                RoundButton {
                    width: row.itemWidth; height: row.itemHeight;
                    imgSource: "qrc:/images/plus.png";
                    imgPressedSource: "qrc:/images/plus2.png";
                    imgDisabledSource: "qrc:/images/plus3.png";

                    property variant dialog

                    isEnabled: true

                    function buttonClicked(){
                        qmlHandler.refreshItems(-1, currentListIndex);
                        appWindow.pageStack.push(addItemPage)
                    }
                }

                RoundButton {
                    width: row.itemWidth; height: row.itemHeight;
                    imgSource: "qrc:/images/minus.png";
                    imgPressedSource: "qrc:/images/minus2.png";
                    imgDisabledSource: "qrc:/images/minus3.png";

                    isEnabled: list.currentIndex > 0

                    function buttonClicked(){
                        var component = Qt.createComponent("MessageBox.qml")
                        messageBox = component.createObject(viewListPage)

                        messageBox.type="Question"
                        messageBox.message=qsTr("Remove item: ") + list.model.get(list.currentIndex).name + qsTr(" from the current list?")
                        messageBox.acceptedClicked.connect(yes)
                        messageBox.open()
                    }

                    function yes(){
                        var selectedGroupIndex=dialogGrupa.selectedIndex
                        qmlHandler.viewList_Delete(currentListIndex, list.model.get(list.currentIndex).itemId, selectedGroupIndex!==-1?dialogGrupa.model.get(selectedGroupIndex).groupId:-1)
                        list.currentIndex = -1;
                        messageBox.destroy()
                    }
                }

                RoundButton {
                    id: buttonEdit
                    width: row.itemWidth; height: row.itemHeight;
                    imgSource: "qrc:/images/editiraj.png";
                    imgPressedSource: "qrc:/images/editiraj2.png";
                    imgDisabledSource: "qrc:/images/editiraj3.png";

                    isEnabled: list.currentIndex > 0

                    function buttonClicked(){
                        var component = Qt.createComponent("ItemDetailsDialog.qml")
                        itemDetailsDialog = component.createObject(viewListPage)

                        itemDetailsDialog.itemName=list.model.get(list.currentIndex).name
                        itemDetailsDialog.quantity=list.model.get(list.currentIndex).quantity

                        itemDetailsDialog.unit=list.model.get(list.currentIndex).unit

                        itemDetailsDialog.message=qsTr("How many of these do you need?")

                        itemDetailsDialog.acceptButtonText=qsTr("Ok")
                        itemDetailsDialog.rejectButtonText=qsTr("Cancel")
                        itemDetailsDialog.accepted.connect(editQuantity)

                        itemDetailsDialog.open()
                    }

                    function editQuantity(quantity, unit){
                        console.log("postavil bi quant na "+quantity+"  "+unit)

                        qmlHandler.viewList_SetQuantity(currentListIndex, list.model.get(list.currentIndex).itemId, quantity, unit)

                        list.model.get(list.currentIndex).quantity=quantity
                        list.model.get(list.currentIndex).unit=unit

                        itemDetailsDialog.destroy()
                    }
                }

                RoundButton {
                    width: row.itemWidth; height: row.itemHeight;
                    imgSource: "qrc:/images/posta.png";
                    imgPressedSource: "qrc:/images/posta2.png";
                    imgDisabledSource: "qrc:/images/posta3.png";

                    isEnabled: list.count!=0

                    function buttonClicked(){
                        qmlHandler.allLists_Message(currentListIndex)
                    }
                }

                RoundButton {
                    id: buttonBought
                    width: row.itemWidth; height: row.itemHeight;
                    imgSource: "qrc:/images/kupljeno.png";
                    imgPressedSource: "qrc:/images/kupljeno2.png";
                    imgDisabledSource: "qrc:/images/kupljeno3.png";

                    isEnabled: list.currentIndex > 0

                    function buttonClicked(){
                        var currentItemIndex=list.currentIndex
                        qmlHandler.viewList_SetBought(list.model.get(currentItemIndex).itemId, currentListIndex, !list.model.get(currentItemIndex).bought)

                        /*
                        ovo radi al refresha cijelu listu
                        var selectedGroupIndex=dialogGrupa.selectedIndex
                        qmlHandler.refreshCurrentList(currentListIndex,selectedGroupIndex!==-1?dialogGrupa.model.get(dialogGrupa.selectedIndex).groupId:-1)

                        list.currentIndex=currentItemIndex*/

                        list.model.get(list.currentIndex).bought=!list.model.get(list.currentIndex).bought
                    }
                }

            }

        }

        //sectionButtonsNormal
        Item{
            id: sectionButtonsNormal

            width: parent.width
            // height: parent.height-sectionButtonsRound.height
            height: grid.rows*(grid.itemHeight)//+grid.spacing

            // color: "yellow"

            anchors {
                horizontalCenter: parent.horizontalCenter
                //top: sectionButtonsRound.bottom
                left: parent.left
                bottom: parent.bottom
                margins: 10
            }

            Grid {
                id: grid

                anchors.fill: parent
                //anchors.margins: 10

                columns: 2
                rows: 1
                spacing: 6

                width: parent.width
                height: parent.height

                property int itemWidth: Math.round((width - spacing * (columns - 1)) / columns)
                property int itemHeight: 51; //Math.round((height - spacing * (rows - 1)) / rows)

                Button{
                    id: buttonBack;
                    text: qsTr("Back");
                    width: grid.itemWidth;
                    height: grid.itemHeight;
                    onClicked: {
                        qmlHandler.refreshAllLists();
                        appWindow.pageStack.pop();
                    }
                }

                Button{
                    id: buttonDeleteBought;
                    text: qsTr("Delete bought");
                    width: grid.itemWidth;
                    height: grid.itemHeight;

                    onClicked: {
                        var component = Qt.createComponent("MessageBox.qml")
                        messageBox = component.createObject(viewListPage)

                        messageBox.type="Question"
                        messageBox.message=qsTr("Delete all bought items?")
                        messageBox.acceptedClicked.connect(yes)
                        messageBox.open()
                    }

                    function yes(){
                        var selectedGroupIndex=dialogGrupa.selectedIndex
                        qmlHandler.viewList_DeleteBought(currentListIndex, selectedGroupIndex!==-1?dialogGrupa.model.get(dialogGrupa.selectedIndex).groupId:-1)
                    }


                }
            }
        }

    }

    MySelectionDialog {
        id: dialogGrupa
        titleText: qsTr("Select group")

        model: modelGroups

        onAccepted: {
            console.log("selected index:"+dialogGrupa.selectedIndex)

            //var selectedGroupIndex=dialogGrupa.selectedIndex



            buttonGrupa.text= dialogGrupa.model.get(dialogGrupa.selectedIndex).name
            //qmlHandler.refreshCurrentList(qmlHandler.getCurrentListIndex(),dialogGrupa.model.get(dialogGrupa.selectedIndex).groupId)
            qmlHandler.refreshCurrentList(currentListIndex,dialogGrupa.model.get(dialogGrupa.selectedIndex).groupId)
            groupIndex = dialogGrupa.selectedIndex
            console.log(list.currentIndex)
        }
    }

    Component{id: addItemPage; AddItemPage {}}
}
